package com.ltmall.api.weixin.pay.utils;

import com.ltmall.api.comm.Path;
import com.ltmall.api.refund.wx.bean.WX;
import com.ltmall.api.refund.wx.json.XmlTOJson;
import com.ltmall.api.refund.wx.util.ClientCustomSSL;
import com.ltmall.api.refund.wx.util.MD5Util;
import com.ltmall.api.refund.wx.util.RequestHandler;
import com.ltmall.api.weixin.pay.Constants;

import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

public class WechatRedPackUtils {
    public static WX sendRedPack(String mchBillno, String openId, int amount) {

        String nonce_str = getNoncestr();
        String partnerkey = Constants.KEY;
        String appid = Constants.OA_APP_ID;
        String mch_id = Constants.OA_MCH_ID;
        String clientIp = Path.hostIp;
        String appsecret = Constants.OA_APP_SECRET;
        String sendName = "乡味汇";
        String totalAmount = amount + "";
        String totalNum = 1 + "";
        String wishing = "乡情红包到账啦！";
        String actName = "乡情红包";
        String remark = "";

        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("wxappid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("mch_billno", mchBillno);
        packageParams.put("send_name", sendName);
        packageParams.put("re_openid", openId);
        packageParams.put("total_amount", totalAmount);
        packageParams.put("total_num", totalNum);
        packageParams.put("wishing", wishing);
        packageParams.put("client_ip", clientIp);
        packageParams.put("act_name", actName);
        packageParams.put("remark", remark);

        RequestHandler reqHandler = new RequestHandler(null, null);
        reqHandler.init(appid, appsecret, partnerkey);

        String sign = reqHandler.createSign(packageParams);
        String xml = "<xml>"
                + "<wxappid>" + appid + "</wxappid>"
                + "<mch_id>" + mch_id + "</mch_id>"
                + "<nonce_str>" + nonce_str + "</nonce_str>"
                + "<sign><![CDATA[" + sign + "]]></sign>"
                + "<mch_billno>" + mchBillno + "</mch_billno>"
                + "<send_name>" + sendName + "</send_name>"
                + "<re_openid>" + openId + "</re_openid>"
                + "<total_amount>" + totalAmount + "</total_amount>"
                + "<total_num>" + totalNum + "</total_num>"
                + "<wishing>" + wishing + "</wishing>"
                + "<client_ip>" + clientIp + "</client_ip>"
                + "<act_name>" + actName + "</act_name>"
                + "<remark>" + remark + "</remark>"
                + "</xml>";
        String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
        try {
            String s = ClientCustomSSL.wxPayRequest(6, url, xml);

            WX wx = XmlTOJson.getWX(s);
            return wx;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 随机字符串
     */
    public static String getNoncestr() {
        Random random = new Random();
        return MD5Util.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }
}
